<template>
  <div>
    <el-form
        label-position="right"
        label-width="100px"
        :model="userInfo"
        style="max-width: 800px"
    >
      <el-input type="hidden" v-model="userInfo.id" />
      <el-form-item label="用户名">
        <el-input disabled v-model="userInfo.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="userInfo.password" />
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="userInfo.confirmPassword" @keyup.enter="submitForm()" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="submitForm()">
          确认修改
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref} from 'vue'
import {ElNotification, FormInstance} from "element-plus";
import {editPassword, getUserInfoByToken} from "../../api/user.js";

const userInfo = reactive({
  id: 0,
  username: '',
  password: '',
  confirmPassword: '',
})

onMounted(()=>{
  getUserInfoByToken().then((res:any) => {
    if(res.code == '200' || res.code == 200){
      userInfo.id = res.data.id;
      userInfo.username = res.data.username;
    }else {
      ElNotification({
        title: 'error',
        message: res.msg,
        type: 'error',
      })
    }
  })
})

const submitForm = async () => {

  if(userInfo.password != userInfo.confirmPassword){
    ElNotification({
      title: 'error',
      message: "两次密码不一致!",
      type: 'error',
    })
    return;
  }

  if (userInfo.password != '' && userInfo.confirmPassword != '') {
    editPassword(userInfo).then((res:any) =>{
      if(res.code == '200' || res.code == 200){
        ElNotification({
          title: 'success',
          message: '修改成功!',
          type: 'success',
        })
      }else {
        ElNotification({
          title: 'error',
          message: res.msg,
          type: 'error',
        })
      }
    })
  } else {
    ElNotification({
      title: 'error',
      message: "密码不能未空!",
      type: 'error',
    })
  }
}

</script>

<style scoped>

</style>
